﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRBIZE
{
    public partial class FMI_TC_USEQRY : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sType = "V";
        private string sSite_No;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;
        private int iGridView2 = 0;
        private int detailindex = -1;
        DataTable myDetail;


        public FMI_TC_USEQRY()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string sDate1 = uiDate1.Text;
            string sDate2 = uiDate2.Text;
            string SQL = "select d.fdictname,b.fpoint_name,c.username,a.fusedate,a.fexplanation"
              + " , s.frfidbarcode,s.fgoodno,e.fgoodname,s.fretqty,s.fuseqty"
              + " ,f.fequipmenttag fboxname, g.fequipmenttag fcabname"
              + " ,a.fbillno,a.fuseuser,a.fplanusedate,a.fmancount,a.fplanbegtime,a.fplanendtime"
              + " ,a.fbillstate,a.fpoint_no"
              + "  from i_tc_use_head a"
              + "  inner join a_ts_point b on b.fpoint_no = a.fpoint_no"
              + "  inner join am_sys_user c on c.id = a.fuseuser"
              + "  left join s_ts_dict d on d.fdicttype = 2007 and d.fdictno = a.fbillstate"
              + "  inner join i_tc_use_detail s on s.fbillno = a.fbillno and s.fuseqty > s.fretqty"
              + "  inner join i_ts_good_info e on e.fgoodno = s.fgoodno"
              + "  inner join i_ts_warehouse f on f.fequipmentbarcode = s.fboxbarcode"
              + "  inner join i_ts_cabinet g on g.fequipmentbarcode = s.fcabbarcode"
              + "  where 1 = 1"
              + "  and a.fusedate between '" + sDate1 + "'"
              + "  and '" + sDate2 + "'";
            DataRowView dr1 = (DataRowView)cbState.SelectedItem;
            if ((dr1 != null) && (cbState.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fbillstate=" + dr1.Row["fdictno"].ToString();
            }

            DataRowView dr2 = (DataRowView)cbfpoint_no.SelectedItem;
            if ((dr2 != null) && (cbfpoint_no.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fpoint_no='" + dr2.Row["fpoint_no"].ToString() + "'";
            }

            DataRowView dr3 = (DataRowView)cbftakeuser.SelectedItem;
            if ((dr3 != null) && (cbftakeuser.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fuseuser='" + dr3.Row["id"].ToString() + "'";
            }

            if (qifbillno.Text != "")
            {
                SQL = SQL + " and a.fbillno like '%" + qifbillno.Text + "%'";
            }

            SQL = SQL + "  order by a.fusedate desc,a.fbillno desc";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();

            
        }

        //执行打印代码
        protected override void OnSys07()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sPrintKey = "'" + uiDGView["fBillNo", rowindex].Value.ToString() + "'";

            base.OnSys07();
        }
        
        private void FMI_TC_TAKEOUT_Load(object sender, EventArgs e)
        {
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys02.Visible = false; sys02.Tag = "1";
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            uiDate1.Text = DateTime.Now.ToString("yyyy-MM-dd");
            uiDate2.Text = DateTime.Now.ToString("yyyy-MM-dd");

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=2007");
            uifbillstate.DataSource = myTs_Dict;
            uifbillstate.DisplayMember = "fDictName";
            uifbillstate.ValueMember = "fDictNo";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fDictName"] = "请选择";
            dr1["fDictNo"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            cbState.DataSource = myTs_Dict;
            cbState.DisplayMember = "fDictName";
            cbState.ValueMember = "fDictNo";
            cbState.SelectedIndex = 0;

            DataTable myUser = myCache.doGetDict("AM_SYS_USER", "");
            uifuseuser.DataSource = myUser;
            uifuseuser.DisplayMember = "username";
            uifuseuser.ValueMember = "id";

            DataRow dr2 = myUser.NewRow();
            dr2["username"] = "请选择";
            dr2["id"] = "0";
            myUser.Rows.InsertAt(dr2, 0);
            cbftakeuser.DataSource = myUser;
            cbftakeuser.DisplayMember = "username";
            cbftakeuser.ValueMember = "id";
            cbftakeuser.SelectedIndex = 0;

            DataTable myStock = myCache.doGetDict("A_TS_POINT", "fpoint_flag=0");
            uifpoint_no.DataSource = myStock;
            uifpoint_no.DisplayMember = "fpoint_name";
            uifpoint_no.ValueMember = "fpoint_no";

            DataRow dr3 = myStock.NewRow();
            dr3["fpoint_name"] = "请选择";
            dr3["fpoint_no"] = "0";
            myStock.Rows.InsertAt(dr3, 0);
            cbfpoint_no.DataSource = myStock;
            cbfpoint_no.DisplayMember = "fpoint_name";
            cbfpoint_no.ValueMember = "fpoint_no";
            cbfpoint_no.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanel1.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifbillno.Text = uiDGView["fbillno", rowindex].Value.ToString();
                string dt = uiDGView["fusedate", rowindex].Value.ToString();
                DateTime mydt = Convert.ToDateTime(dt);
                uifusedate.Text = mydt.ToString("yyyy-MM-dd");
                dt = uiDGView["fplanusedate", rowindex].Value.ToString();
                mydt = Convert.ToDateTime(dt);
                uifplanusedate.Text = mydt.ToString("yyyy-MM-dd");
                uifexplanation.Text = uiDGView["fexplanation", rowindex].Value.ToString();
                uifplanbegtime.Text = uiDGView["fplanbegtime", rowindex].Value.ToString();
                uifplanendtime.Text = uiDGView["fplanendtime", rowindex].Value.ToString();
                uifmancount.Text= uiDGView["fmancount", rowindex].Value.ToString();

                int i = 0;
                string sfbillstate = uiDGView["fbillstate", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifbillstate.Items)
                {
                    string id = dr["fdictno"].ToString();
                    if (id.Equals(sfbillstate))
                    {
                        uifbillstate.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfuseuser = uiDGView["fuseuser", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifuseuser.Items)
                {
                    string id = dr["id"].ToString();
                    if (id.Equals(sfuseuser))
                    {
                        uifuseuser.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfpoint_no = uiDGView["fpoint_no", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifpoint_no.Items)
                {
                    string id = dr["fpoint_no"].ToString();
                    if (id.Equals(sfpoint_no))
                    {
                        uifpoint_no.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }

            //载入明细
            string sql = "select fbillno,fline,frfidbarcode,a.fgoodno,b.fgoodname,b.fspec"
               +" , a.fbuyprice,fbalqty,fretqty,fuseqty,fboxbarcode,fcabbarcode,fprocdate"
               + " ,c.fequipmenttag fboxname, d.fequipmenttag fcabname"
               + " from i_tc_use_detail a"
               + " left join i_ts_good_info b on a.fgoodno = b.fgoodno"
               + " left join i_ts_warehouse c on a.fboxbarcode = c.fequipmentbarcode"
               + " left join i_ts_cabinet d on a.fcabbarcode = d.fequipmentbarcode"
               + " where 1 = 1"
               + " and a.fbillno='" + uifbillno.Text + "'"
               + " order by fline";
            myDetail = StaticClass.myDB.query(sql);
            uiDetail.DataSource = myDetail;

            //表格列显示统一控制
            if (iGridView2 == 0)
            {
                myCache.doSetGridVIew(sMenu + "1", sUser, uiDetail);
                iGridView2 = iGridView2 + 1;
            }

        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        private void uiDetail_DoubleClick(object sender, EventArgs e)
        {
            string iMenu = sMenu+"1";
            OnGridDbClick(iMenu, uiDetail);
        }

        private void uiDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            detailindex = e.RowIndex;
        }

        private void cbState_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbfpoint_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbftakeuser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
